package com.example.crm.controller;

import com.example.crm.entity.Chance;
import com.example.crm.entity.Product;
import com.example.crm.service.ProductService;
import com.example.crm.util.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/crm/product")
public class ProductController {

    @Resource
    private ProductService productService;


    @RequestMapping("/list")
    public String findAll(Model model, @RequestParam(required = false)String prodName,
                          @RequestParam(required = false)String prodType,
                          @RequestParam(required = false)String prodBatch, Integer currPageNo){

        Integer page1 = currPageNo==null?1:currPageNo;

        Page page = new Page();
        page.setTotal(productService.count(prodName,prodType,prodBatch)); // 数据总条数
        if (page1<1){
            page1=1;
            model.addAttribute("message","当前已是最顶一页");
        }
        if (page1>page.getPages()){
            page1=page.getPages();
            model.addAttribute("message","当前已是最后一页");
        }
        page.setCurrent(page1); // 设置偏移量
        // 存储数据
        List<Product> all = productService.findAll(prodName,prodType,prodBatch, page.getOffset(), page.getPageSize());
        page.setList(all);
        model.addAttribute("productPage",page);
        return "product/list";
    }
}
